/****************************************************
 * 创建人: @author zoujian    
 * 创建时间: 2021-6-22/15:25
 * 项目名称: design_pattern
 * 文件名称: ReentrantLockTest3.java
 * 文件描述: @Description: 响应中断
 *
 * All rights Reserved, Designed By 投资交易团队
 * @Copyright:2016-2021
 *
 ********************************************************/
package com.zj.designpattern.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 包名称：com.zj.designpattern.lock
 * 类名称：ReentrantLockTest3
 * 类描述：响应中断
 * 创建人：@author zoujian
 * 创建时间：2021-6-22/15:25
 */
public class ReentrantLockTest3 {
    static Lock lock1 = new ReentrantLock();
    static Lock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadDemo(lock1,lock2));
        Thread thread1 = new Thread(new ThreadDemo(lock2,lock1));
        thread.start();
        thread1.start();
        thread1.interrupt();
    }


    static class ThreadDemo implements Runnable{
        Lock firstLock;
        Lock secondLock;

        public ThreadDemo(Lock firstLock, Lock secondLock) {
            this.firstLock = firstLock;
            this.secondLock = secondLock;
        }

        @Override
        public void run() {
            try {
                firstLock.lockInterruptibly();
                TimeUnit.MILLISECONDS.sleep(50);
                secondLock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                firstLock.unlock();
                secondLock.unlock();
                System.out.println(Thread.currentThread().getName() + ":获取到资源，正常结束了");
            }
        }
    }
}


